<?php
//支付设置
namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\SystemSet;
use app\common\model\HuiYuanSupervise;
use think\Db;
use think\Request;


class Paysetting extends Base
{
	protected $system;
	protected $huiyuan;
	public function _initialize()
	{
		parent::_initialize();
		$this->system = new SystemSet();
		$this->huiyuan = new HuiYuanSupervise();
	}

	public function cz_fa_list($xx_name='',$xx_test='',$xx_remark='',$file='')
	{
		$zfid = input('zfid')?input('zfid'):1;
		
		if(input('post.')){
			$where=array('id'=>$zfid);
			$q = $this->system->dFind('pay_xxlist',$where);
			$data=array();
			if($xx_name!=''){
				$data['xx_name']=$xx_name;
				$data['xx_remark']=$xx_remark;
				$data['xx_test']=$xx_test;
				$data['xx_erweima']=$file?$file:$q['xx_erweima'];
				$data['status'] = intval( input('post.status') );
			}

			if($q['id']){
				$this->huiyuan->SaveUpdate('pay_xxlist', $where, $data);
				$this->huiyuan->caozuorizhi_xieru('修改线下支付账户:“'.$xx_name.'”账户信息');
				$this->system->alertWin('操作成功', '/admin/paysetting/payList?ticker=0');
			}else{
				js_tishi('操作失败', '/admin/paysetting/cz_fa_list?zfid='.$zfid);
			}
		}
		$where = array('id'=>$zfid);
		$query = $this->system->dFind('pay_xxlist',$where);
		$this->assign('zfid',$zfid);
		$this->assign('query',$query);
		return $this->fetch();
	}
	public function DelChongzhi(){
		$zfid = input('zfid');
		$name=Db::name('pay_xxlist')->where('id',$zfid)->find();
		if($zfid){
			Db::name('pay_xxlist')->where('id',$zfid)->delete();
			$this->huiyuan->caozuorizhi_xieru('删除线下支付账户:'.$name['xx_name']);
			js_tishi('删除成功', 'payList?ticker=0');
		}
	}
	public function updatePayOrder() {
		$name = input('post.name') ? input('post.name') : '';
		$value = input('post.value') ? input('post.value') : '';
		if(!$name) {
			return 0;
		}
		 $result=Db::name('pay_type_list')->where(array('id'=>$name))->find();
	    $fangan=Db::name('pay_xianshang')->where('id',$result['method'])->find();
		$res = Db::name('pay_type_list')->where(array('id'=>$name))->update(array('order'=>$value));
		if($res === 1) {
			$this->huiyuan->caozuorizhi_xieru('设置“'.$fangan['pay_name'].'-'.$result['type_name'].'”的排续号码为'.$value);
			return 1;
		} else {
			return 0;
		}
	}
	
	//更新线上方案支持状态
	public function updateSupport() {
	    $id = input('post.id') ? intval(input('post.id')) : 0;
	    $name = input('post.name') ? input('post.name') : '';
	    $status = input('post.status')=='true' ? 1 : 0;
	    if(!$name || !$id) {
	        return 0;
	    }
	    if($name=='pc'){
	    	$tname="电脑端";
	    }
	    if($name=='mob'){
	    	$tname="手机端";
	    }
	    $result=Db::name('pay_type_list')->where(array('id'=>$id))->find();
	    $fangan=Db::name('pay_xianshang')->where('id',$result['method'])->find();
	    $res = Db::name('pay_type_list')->where(array('id'=>$id))->update(array($name=>$status));
	    if($res === 1) {
	    	
	    	if($status==0){
	    		$this->huiyuan->caozuorizhi_xieru('设置“'.$fangan['pay_name'].'-'.$result['type_name'].'”不支持'.$tname.'支付');
	    	}
	    	if($status==1){
	    		$this->huiyuan->caozuorizhi_xieru('设置“'.$fangan['pay_name'].'-'.$result['type_name'].'”支持'.$tname.'支付');
	    	}
	        return 1;
	    } else {
	        return 0;
	    }
	}
	
	//更新线上二维码账户禁用状态
	public function updateLock() {
	    $id = input('post.id') ? intval(input('post.id')) : 0;
	    $name = input('post.name') ? input('post.name') : '';
	    $value = input('post.value');
	    if(!$name || !$id || !isset($value)) {
	        return 0;
	    }
	    $res = Db::name('code_account')->where(array('id'=>$id))->update(array($name=>$value));
	    $zhanghu=Db::name('code_account')->where(array('id'=>$id))->find();
	    if($res === 1) {
	    	if($value==0){
	    		$this->huiyuan->caozuorizhi_xieru('设置二维码账户“'.$zhanghu['name'].'”的状态为“正常”');
	    	}
	    	if($value==1){
	    		$this->huiyuan->caozuorizhi_xieru('设置二维码账户“'.$zhanghu['name'].'”的状态为“禁用”');
	    	}
	        return 1;
	    } else {
	        return 0;
	    }
	}
	
	public function updateOpen(){
		$open = input('post.open');
		$id = input('post.uid');
		$name=Db::name('pay_xianshang')->where('id',$id)->find();
		if($open == '开'){$va=1;}
		if($open == '关'){$va=0;}
		if(Db::name('pay_xianshang')->where('id',$id)->update(array('open'=>$va))){
			if($va==0){
				$this->huiyuan->caozuorizhi_xieru('将支付方案“'.$name['pay_name'].'”的开关设置为关');
			}
			if($va==1){
				$this->huiyuan->caozuorizhi_xieru('将支付方案“'.$name['pay_name'].'”的开关设置为开');
			}
			return 1;
		}else{
			return 2;
		}
		
	}
	public function updateOff(){
		$id = input('post.id');
		$status = input('post.status');
		$name=Db::name('pay_xxlist')->where('id',$id)->find();
		if(Db::name('pay_xxlist')->where('id',$id)->update(array('status'=>1-intval($status)))){
			if($status==0){
				$this->huiyuan->caozuorizhi_xieru('将支付方案“'.$name['xx_name'].'”的开关设置为开');
			}
			if($status==1){
				$this->huiyuan->caozuorizhi_xieru('将支付方案“'.$name['xx_name'].'”的开关设置为关');
			}
			return 1;
		}else{
			return 0;
		}

	}
	public function updateOpenM(){
		$open = input('post.open');
		$id = input('post.id');
		$name=Db::name('pay_method')->where('id',$id)->find();
		if($open == '开'){$va=1;}
		if($open == '关'){$va=0;}
		if(Db::name('pay_method')->where('id',$id)->update(array('status'=>$va))){
			if($va==0){
				$this->huiyuan->caozuorizhi_xieru('将支付类型“'.$name['name'].'”的开关设置为关');
			}
			if($va==1){
				$this->huiyuan->caozuorizhi_xieru('将支付类型“'.$name['name'].'”的开关设置为开');
			}
			return 1;
		}else{
			return 2;
		}

	}
	function updateOnLineStatus(){
		$open = input('post.open');
		$id = input('post.uid');
		$name=Db::name('code_account')->where('id',$id)->find();
		if($open == '开'){
			$va=0;
		}else{
			$va=1;
		}
		if(Db::name('code_account')->where('id',$id)->update(array('lock'=>$va))){
			if($va==0){
				$this->huiyuan->caozuorizhi_xieru('将支付类型“'.$name['name'].'”的开关设置为开');
			}
			if($va==1){
				$this->huiyuan->caozuorizhi_xieru('将支付类型“'.$name['name'].'”的开关设置为关');
			}
			return 1;
		}else{
			return 2;
		}
	}
	public function payList(){
		$ticker = (input('ticker')!==NULL) ? input('ticker') : '2';
		$where=array();
		$pay = Db::name('pay_xianshang')->where(['is_code'=>0])->order('id asc')->select();
		if(input('post.')){
			$open = input('open',-1);
			if(input('method')==0){
				$where=array('open'=>$open);
			}else{
				$where=array('id'=>input('method'),'open'=>$open);
			}
			if( -1==$open ){
				unset($where['open']);
			}
		}
		if( 0==$ticker ){
			$xxpay = Db::name('pay_xxlist')->order('id desc')->paginate(15,false,['query'=>request()->param()]);
		}elseif( 1==$ticker ){
			$xxpay = Db::name('pay_xianshang')->order('id asc')->where($where)->where(['is_code'=>0])->paginate(15,false,['query'=>request()->param()]);
		}elseif( 3==$ticker ){
			$xxpay = Db::name('pay_xianshang')->order('id asc')->where($where)->where(['is_code'=>1])->paginate(15,false,['query'=>request()->param()]);
		}else{
			$xxpay = Db::name('pay_method')->order('seq asc')->where($where)->paginate(15,false,['query'=>request()->param()]);
		}

		$method = Db::name('pay_method')->column('id,name');
		$this->assign('paymethod',$method);
		$this->assign('pay',$pay);
		$this->assign('xxpay',$xxpay);
		$this->assign('ticker',$ticker);
		$this->assign('method',input('method'));
		$this->assign('open',input('open'));
		return $this->fetch('pay_list');
	}
	public function jurisdiction(){
	    $id = input('get.id');
	    $name=Db::name('pay_xianshang')->where('id',$id)->find();
	    if(input('post.')){
	        $lv_id = implode(',', input('post.lv_id/a'));
	        $save = Db::name('pay_xianshang')->where('id',$id)->update(['level'=>$lv_id]);
	        if($save){
	        	$this->huiyuan->caozuorizhi_xieru('修改线上支付方案'.$name['pay_name'].'的等级权限');
	            $this->success('修改成功！');
	        }else{
	            $this->error('修改失败！');
	        }
	        
	    }else{
	        $level = Db::name('us_class')->field('id,class_name')->select();
	         
	        $pay = Db::name('pay_xianshang')->where('id',$id)->find();
	        $level_pay = explode(',', $pay['level']);
	        $this->assign('level_pay',$level_pay);
	        $this->assign('level',$level);
	        return $this->fetch();
	    }
	}
	
	public function addPay(){
	    $this->redirect('/admin/Paysetting/payList');exit;
		$us_class = Db::name('us_class')->field('id,class_name')->select();
		$data = array();
		if(input('post.')){
			$data['pay_name'] = input('payname');
			$data['level'] = implode(',', input('post.usclass/a'));
			if(Db::name('pay_xianshang')->insert($data)){
				js_tishi('新增成功', 'payList?ticker=1');
			}else{
				js_tishi('新增失败', 'payList?ticker=1');
			}
		}
		$this->assign('us_class',$us_class);
		return $this->fetch('add_pay');
	}
	
    /**
	 * 线上支付方式详情
	 */
	public function payDetail() {
	    $id = input('get.id') ? intval(input('get.id')) : 0;
	    $query = Db::name('pay_type_list')->alias('pt')
	                    ->join('__PAY_XIANSHANG__ px','px.id=pt.method')
	                    ->field('pt.*,px.pay_name')
	                    ->where('pt.method',$id)
                	    ->paginate(10,false,['query'=>request()->param()]);
		$method = Db::name('pay_method')->column('id,name');
	    $this->assign('query',$query);
	    $this->assign('method',$method);
	    return $this->fetch('pay_detail');
	}
	
	/**
	 * 二维码支付账户详情
	 */
	public function payAccount() {
	    $id = input('get.id') ? intval(input('get.id')) : 0;
	    $where = [];
	    if($id == 3) $where['type'] = 'wechat';
	    if($id == 4) $where['type'] = 'alipay';
	    
	    $query = Db::name('code_account')->where($where)->paginate(10);
		$method = Db::name('pay_method')->column('id,name');
		$this->assign('query',$query);
		$this->assign('method',$method);
	    return $this->fetch('pay_account');
	}
	
	/**
	 * 线上支付方式详情
	 */
	public function codeList() {
	    $id = input('get.id') ? intval(input('get.id')) : 0;
	    $query = Db::name('response_code')->alias('rc')
                	    ->join('__CODE_ACCOUNT__ ca','ca.id=rc.account')
                	    ->field('rc.*,ca.name')
                	    ->where('rc.account',$id)
                	    ->paginate(10,false,['query'=>request()->param()])
                	    ->each(function($item,$key){
                	    	if($item['type'] == 'wechat') $item['type'] = '微信';
	                        if($item['type'] == 'alipay') $item['type'] = '支付宝';
	                        if($item['status']) $item['status'] = '当前可用';
	                        if(!$item['status']) $item['status'] = '已被占用';
	                        return $item;
                	    });
	    $this->assign('query',$query);
	    return $this->fetch('code_list');
	}
	
	public function codeDel(){
	    $codeid = input('get.codeid');
	    $id = input('get.id');
	    $find = Db::name('response_code')->where('id',$codeid)->find();
	    $name=Db::name('code_account')->where('id',$find['account'])->find();
	    @unlink(ROOT_PATH.'public/uploads/code/'.$find['code']);
	    
	    $del = Db::name('response_code')->where('id',$codeid)->delete();
	    if($del){
	    	$this->huiyuan->caozuorizhi_xieru('线上二维码账户“'.$name['name'].'”删除一张二维码');
	        js_tishi('删除成功！','/admin/Paysetting/codeList.html?id='.$id);
	    }else{
	        alert_back('删除失败！');
	    }
	}
	
	
	public function cz_fa_new($xx_name='',$xx_remark='',$xx_test='',$file='')
	{
		$zfid = input('zfid')?input('zfid'):1;
		if(input('post.')){
			$where=array('id'=>$zfid);
			$q = $this->system->dFind('pay_xxlist',$where);
			$data=array();
			if($xx_name!=''){
				$data['xx_name']=$xx_name;
				$data['xx_remark']=$xx_remark;
				$data['xx_test']=$xx_test;
				$data['xx_erweima']=$file?$file:$q['xx_erweima'];
				$data['status'] = intval( input('post.status') );
			}

			if($q['id']){
				$this->huiyuan->SaveUpdate('pay_xxlist', $where, $data);
				$this->system->alertWin('操作成功', '/admin/paysetting/payList?ticker=0');
			}else{
				js_tishi('操作失败', '/admin/paysetting/cz_fa_new?zfid='.$zfid);
			}
		}
		$where = array('id'=>$zfid);
		$query = $this->system->dFind('pay_xxlist',$where);
		$this->assign('zfid',$zfid);
		$this->assign('query',$query);
		return $this->fetch();
	}

	public function cz_qq($xx_name='',$xx_remark='',$xx_test='',$file='')
	{
		$zfid = input('zfid')?input('zfid'):1;
		if(input('post.')){
			$where=array('id'=>$zfid);
			$q = $this->system->dFind('pay_xxlist',$where);
			$data=array();
			if($xx_name!=''){
				$data['xx_name']=$xx_name;
				$data['xx_remark']=$xx_remark;
				$data['xx_test']=$xx_test;
				$data['status'] = intval( input('post.status') );
				$data['xx_erweima']=$file?$file:$q['xx_erweima'];
			}
			if($q['id']){
				$this->huiyuan->SaveUpdate('pay_xxlist', $where, $data);
				$this->system->alertWin('操作成功', '/admin/paysetting/payList?ticker=0');
			}else{
				js_tishi('操作失败', '/admin/paysetting/cz_qq?zfid='.$zfid);
			}
		}
		$where = array('id'=>$zfid);
		$query = $this->system->dFind('pay_xxlist',$where);
		$this->assign('query',$query);
		$this->assign('zfid',$zfid);

		return $this->fetch();
	}

	public function cz_dsf_list()
	{
		if(input('post.')){
			$where=array('id'=>input('zfid'));
			$q = $this->system->dFind('pay_xxlist',$where);
			$data=array();
			$name = trim(input('post.xx_name'));
			$ename = trim(input('post.xx_test'));
			$bank_name = trim(input('post.xx_bankname')) ? input('post.xx_bankname') : '';
			$bankno = trim(input('post.xx_bankno')) ? input('post.xx_bankno') : '';
			$bankren = trim(input('post.bankren')) ? input('post.bankren') : '';
			$method_id = trim(input('post.method_id')) ? input('post.method_id') : 0;
			$xx_remark = input('xx_remark')?input('xx_remark'):'bank';
			if($bankren!=''){
				$data = array(
	                'xx_name' => $name,
	                'xx_test'   => $ename,
	                'xx_bankname'   => $bank_name,
	                'xx_bankno'   => $bankno,
	                'xx_bankren'   => $bankren,
	                'method_id' => $method_id,
					'xx_remark' =>$xx_remark,
					'status' => intval( input('post.status') )
	        	);
			}
			if($q['id']){
				$this->huiyuan->SaveUpdate('pay_xxlist', $where, $data);
				$this->system->alertWin('操作成功', '/admin/paysetting/payList?ticker=0');
			}else{
				js_tishi('操作失败', '/admin/paysetting/cz_dsf_list?zfid='.input('zfid'));
			}
		}
		$query = $this->system->dLists('bank');
		$where = array('id'=>input('zfid'));
		$t = $this->system->dFind('pay_xxlist',$where);
		$method = Db::name('pay_method')->column('id,name');
		$this->assign('method',$method);
		$this->assign('t',$t);
		$this->assign('query',$query);
		return $this->fetch();
	}
	
	public function cz_dsf_new()
	{
		return $this->fetch();
	}
	
	public function cz_xx_list()
	{
		return $this->fetch();
	}
	
	public function cz_xx_new()
	{
		return $this->fetch();
	}
	
	//添加二维码收款账户
	public function addCodeAccount() 
	{
		$id = input('id');
		$query = Db::name('pay_xianshang')->where('id',$id)->value('pay_name');
	    if(input('post.') && input('get.is_add')==1) {
	        $name = input('post.name') ? input('post.name') : '';
	        $type = input('post.type') ? input('post.type') : '';
	        $order = input('post.order') ? input('post.order') : '';
	        $label = input('label','');
	        if(!$name || !$type || !in_array($type,['alipay','wechat'])) {
				alert_back('信息错误');
	        }
	        $have = Db::name('code_account')->where(['type'=>$type,'name'=>$name])->find();
	        if($have) {
				alert_back("该账户已存在");
	        }

			if( !preg_match("/^[a-zA-Z\s]+$/",$label) || 3!=strlen($label) ){
				alert_back("标识格式不对");
			}

			$xs3 = Db::name('pay_xianshang')->where(['is_code'=>0])->column('label');
			$xx = Db::name('pay_xxlist')->where('label!=""')->column('label');
			$xs4 = Db::name('code_account')->where('label!=""')->column('label');

			$can_use = array_merge($xs3,$xx,$xs4);

			if( in_array($label,$can_use) ){
				alert_back("唯一标识不可用");
			}
	        $data = array(
	        	'name' => $name,
                'type'   => $type,
                'label'   => $label,
                'order' => $order,
				'method_id' => input('post.method_id') ? input('post.method_id') : 0
	        );
	        Db::name('code_account')->insert($data);
	        $this->huiyuan->caozuorizhi_xieru('添加线上二维码收款账户:'.$name);
			$this->system->alertLayer('操作成功');
	    }
		$method = Db::name('pay_method')->column('id,name');
		$this->assign('type_value',$query);
		$this->assign('method',$method);
	    return $this->fetch('add_code_account');
	}
	
	//添加线下收款账户
	public function addOfflineAccount()
	{
	    if(input('post.') && input('get.is_add')==1) {
	        $name = trim(input('post.xx_name'));
	        $ename = trim(input('post.xx_test'));
	        $type = trim(input('post.type'));
	        $bank_name = trim(input('post.bank_name')) ? input('post.bank_name') : '';
	        $bankno = trim(input('post.bankno')) ? input('post.bankno') : '';
	        $bankren = trim(input('post.bankren')) ? input('post.bankren') : '';
	        $method_id = trim(input('post.method_id')) ? input('post.method_id') : 0;
			$remark = input('xx_remark','');
			$label = input('label','');
			$status = input('status',1);
	        if(!in_array($type,['ali','wechat','bank','qq'])) {
	            $this->system->alertWin('信息错误', '/admin/paysetting/addOfflineAccount');exit;
	        }

			if( !preg_match("/^[a-zA-Z\s]+$/",$label) || 3!=strlen($label) ){
				alert_back("标识格式不对");
			}


			$xs3 = Db::name('pay_xianshang')->where(['is_code'=>0])->column('label');
			$xx = Db::name('pay_xxlist')->where('label!=""')->column('label');
			$xs4 = Db::name('code_account')->where('label!=""')->column('label');

			$can_use = array_merge($xs3,$xx,$xs4);

			if( in_array($label,$can_use) ){
				$this->system->alertWin('唯一标识不可用', '/admin/paysetting/addOfflineAccount');exit;
			}
			$code = $this->system->uploadUrl('xx_code');
	        $data = array(
	                'xx_name' => $name,
	                'xx_weixin' => $type == 'wechat' ? 1 : 0,
	                'xx_zhifubao' => $type == 'ali' ? 1 : 0,
	                'xx_wangyin' => $type == 'bank' ? 1 : 0,
	                'xx_qq' => $type == 'qq' ? 1 : 0,
	                'xx_test'   => $ename,
	                'xx_bankname'   => $bank_name,
	                'xx_bankno'   => $bankno,
	                'xx_bankren'   => $bankren,
	                'xx_erweima'   => $code,
	                'xx_remark' => $remark,
	                'label' => $label,
	                'method_id' => $method_id,
	                'status' => intval($status)
	        );
	        Db::name('pay_xxlist')->insert($data);
	        $this->huiyuan->caozuorizhi_xieru('添加线下收款账户:'.$name);
	        $this->system->alertWin('操作成功', '/admin/paysetting/payList?ticker=0');
	    }

		$method = Db::name('pay_method')->column('id,name');
		$this->assign('method',$method);
	    return $this->fetch('add_offline_account');
	}
	
	//添加收款二维码
	public function addCode()
	{
	    if(input('post.')) {
	        $file_name = request()->file('file')->getInfo()['name'];
	        $file_name = str_replace(strrchr($file_name, "."),"",$file_name);
	        if(!is_numeric($file_name)) {
	            return json(array('status'=>0,'error'=>'文件名必须对应二维码的金额'));
	        }
	        $account_id = input('get.pay_type') ? input('get.pay_type') : 0;
	        $money = $file_name ? $file_name : '';
	        $region = $file_name ? intval($file_name) : '';
	        
	        if($region < 1) {
	            return json(array('status'=>0,'error'=>'最小金额不能小于1元'));
	        }
	        
            $type = Db::name('code_account')->where('id',$account_id)->value('type');
            
            
	        if(!$money || !$type || !$region) {
	            return json(array('status'=>0,'error'=>'信息错误'));
	        }
	        $have = Db::name('response_code')->where(['type'=>$type,'account'=>$account_id,'money'=>$money])->find();
	        $code = $this->system->uploadUrl('file','uploads/code');
	        if($have) {
	            Db::name('response_code')->where('id',$have['id'])->setField('code',$code);
	            @unlink(ROOT_PATH.'public/uploads/code/'.$have['code']);
	            return json(array('status'=>1,'success'=>'上传成功'));
	        }
	        $data = array(
	                'account' => $account_id,
	                'type'   => $type,
	                'money' => $money,
	                'region' => $region,
	                'code' => $code,
	                'czadmin' => cookie('user'),
	                'addtime' => time()
	        );
	        $insert = Db::name('response_code')->insert($data);
	        $name=Db::name('code_account')->where('id',$account_id)->find();
	        if($insert){
	        	$this->huiyuan->caozuorizhi_xieru('给二维码收款账户:'.$name['name'].'添加二维码');
	             return json(array('status'=>1,'success'=>'上传成功'));
	        }else{
	            return json(array('status'=>0,'error'=>'上传失败'));
	        }
	        
	    }
	    $id = input('id');
	    $account_info = Db::name('code_account')->where('lock',0)->where('id',$id)->select();
	    foreach($account_info as &$v) {
	        if($v['type']=='wechat') $v['type'] = '微信';
	        if($v['type']=='alipay') $v['type'] = '支付宝';
	    }
	    $this->assign('account',$account_info);
	    return $this->fetch('add_code');
	}
	public function addPayMethod(){

		if(input('post.') && input('get.is_add')==1) {
			$id = input('post.id',0);
			$this->assign('id',$id);
			$name = input('post.name','');
			$remark = input('post.remark','');
			$seq = input('post.seq','');
			$status = input('post.status',0);
			$method = input('post.method',0);
			$money = input('post.money',0);
			$data = array(
					'name' => $name,
					'remark' => $remark,
					'seq' => $seq,
					'method' => $method,
					'money' => $money,
					'status' => $status,
					'create_time' => time()
			);
			$code = $this->system->uploadUrl('logo');
			if( !empty($code) ) $data['logo'] = $code;
			if( empty($id) ){
				if(Db::name('pay_method')->insert($data)){
					$this->huiyuan->caozuorizhi_xieru('添加支付类型:'.$name);
					$this->system->alertWin('操作成功', '/admin/paysetting/payList?ticker=2');
				}
			}else{
				if(Db::name('pay_method')->where(['id'=>$id])->update($data)){
					$this->huiyuan->caozuorizhi_xieru('修改支付类型:'.$name);
					$html = 'alert("操作成功");';
					$html .= 'parent.location.reload();';
					$html .= 'var index = parent.layer.getFrameIndex(window.name);';
					$html .= 'parent.layer.close(index);';
					 // 父页面刷新
					 //获取窗口索引
					echo "<script>{$html}</script>";
					return;
				}
			}
			echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
			echo "<script>alert('操作失败！');</script>";
		}
		$id = input('id',0);
		$detail = ['name'=>'','status'=>1,'logo'=>'','id'=>0,'seq'=>0,'remark'=>'','method'=>0,'money'=>0];
		if( $id ){
			$detail = Db::name('pay_method')->find($id);
		}
		$this->assign('detail',$detail);
		return $this->fetch('add_pay_method');
	}
	public function checkMethod(){
		$id = input('post.id',0);
		$val = input('post.value',0);
		$type = input('post.type',0);
		if( empty($id) || empty($type) ){
			return json_encode(['status'=>0,'msg'=>"无效信息"]);
		}
		$table = '';
		if( 1==$type ){
			$table = 'pay_type_list';
			$result=Db::name($table)->where('id',$id)->find();
			$name=$result['type_name'];
		}elseif( 2==$type ){
			$table = 'code_account';
			$result=Db::name($table)->where('id',$id)->find();
			$name=$result['name'];
		}elseif( 3==$type ){
			$table = 'pay_xxlist';
			$result=Db::name($table)->where('id',$id)->find();
			$name=$result['xx_name'];
		}

		if( Db::name($table)->where(['id'=>$id])->update(['method_id'=>$val]) ){
			
			$this->huiyuan->caozuorizhi_xieru('修改“'.$name.'”的所属支付');
			return json_encode(['status'=>1,'msg'=>"更改成功"]);
		}else{
			return json_encode(['status'=>0,'msg'=>"更改失败"]);
		}

	}
	public function methodList(){
		$id = input('id',0);
		$type = input('type',2);
		//线上第三方
		if( 0==$type ){
			$query = Db::name('pay_type_list')->alias("a")
					->join("__PAY_XIANSHANG__ b",'a.method=b.id','LEFT')
					->where(['a.method_id'=>$id,'b.open'=>1])
					->field("a.id,b.pay_name as name_2,a.type_name as name_1,a.pc,a.mob,b.id as pid")
					->order('id asc')
					->paginate(10,false,['query'=>request()->param()]);
		}elseif( 1==$type ){

			$query = Db::name('code_account')->alias("a")
					->join("__PAY_XIANSHANG__ b",'a.type=b.label','LEFT')
					->where(['a.method_id'=>$id,'b.open'=>1,'b.is_code'=>1])
					->field("a.id,a.name as name_1,a.type as name_2,a.lock,b.id as pid")
					->order('a.id asc')
					->paginate(10,false,['query'=>request()->param()])->each(function($item){
						if( 'alipay'==$item['name_2'] ){
							$item['name_2'] = '支付宝';
						}else{
							$item['name_2'] = '微信';
						}
						return $item;
					});
		}else{
			$query = Db::name('pay_xxlist')
					->where(['method_id'=>$id])
					->field("id,xx_name as name_1,xx_weixin,xx_zhifubao,xx_qq,xx_wangyin,0 as name_2,status,xx_bankno,xx_erweima ")
					->order('id asc')
					->paginate(10,false,['query'=>request()->param()])->each(function($item){
						if( 1==$item['xx_weixin'] )  $item['name_2'] = '微信';
						if( 1==$item['xx_zhifubao'] ) $item['name_2'] = '支付宝';
						if( 1==$item['xx_wangyin'] ) $item['name_2'] = '网银';
						if( 1==$item['xx_qq'] ) $item['name_2'] = 'QQ';
						return $item;
					});
		}
		$this->assign('query',$query);
		$this->assign('id',$id);
		$this->assign('type',$type);
		return $this->fetch('method_list');
	}
	public function bank(){
		$list = Db::name('bank')->paginate(10,false,['query'=>request()->param()]);
		$this->assign('list',$list);
		return $this->fetch('bank');
	}
	public function addBank(){
		$id = input('id',0);
		if( Request::instance()->isPost() ){
			$name = input('bankname','');
			if( empty($id) && Db::name('bank')->insert(['bankname'=>$name]) ){
				$this->huiyuan->caozuorizhi_xieru('新增银行:'.$name);
				$this->system->alertLayer('编辑成功');
			}elseif( !empty($id) && Db::name('bank')->where(['id'=>$id])->update(['bankname'=>$name]) ){
				$this->huiyuan->caozuorizhi_xieru('修改银行:'.$name);
				$this->system->alertLayer('编辑成功');
			}else{
				$this->system->alertWin('编辑失败', '/admin/paysetting/addBank?id='.$id);exit;
			}
		}
		$detail = ['id'=>0,'bankname'=>''];
		if( !empty($id) ){
			$detail = Db::name('bank')->find($id);
		}
		$this->assign('detail',$detail);
		return $this->fetch('add_bank');
	}
	public function delBank(){
		$id = input('id',0);
		if( !empty($id) ){
			$count = Db::name('username')->where(['bankname'=>$id])->count();
			if( $count > 0 ){
				return 2;	//该银行被占用
			}
			$name=Db::name('bank')->where(['id'=>$id])->find();
			if( Db::name('bank')->where(['id'=>$id])->delete() ){

				$this->huiyuan->caozuorizhi_xieru('删除银行:'.$name['bankname']);
				return 1;	//删除成功
			}
		}
		return 0;	//删除失败
	}
}